package com.wyix.service;

import com.wyix.domain.Order;

import java.util.Comparator;
import java.util.List;

/**
 * @author wangdx
 * @since 2025/8/5
 */
public class OrderService {
    // com.example.service.OrderService.java
    public List<Order> sortOrdersByPrice(List<Order> orders) {
        // Lambda实现Comparator接口
        orders.sort((o1, o2) -> Double.compare(o1.getPrice(), o2.getPrice()));
        return orders;
    }

    public List<Order> sortOrdersByTimeDesc(List<Order> orders) {
        // 方法引用替代Lambda
        orders.sort(Comparator.comparing(Order::getCreateTime).reversed());
        return orders;
    }

    // com.example.service.OrderService.java
    public double calculateUnpaidTotal(List<Order> orders) {
        return orders.stream()
                .filter(order -> !order.isPaid())  // Lambda作为过滤条件
                .mapToDouble(Order::getPrice)       // 方法引用替代Lambda
                .sum();
    }
}
